import json

from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View

from homes.models import House
from order.models import Order
from settings import QINIU_URL
from users.models import User

#订单
class OrderView(View):
    def post(self,request):
        house_id = json.loads(request.body.decode()).get("house_id")
        start_date = json.loads(request.body.decode()).get("start_date")
        end_date = json.loads(request.body.decode()).get("end_date")
        days = json.loads(request.body.decode()).get("days")
        username = request.session.get('username')
        try:
            user = User.objects.get(mobile=username)
        except:
            return http.JsonResponse({"errno": "4101", "errmsg": "未登录"})
        try:
            house = House.objects.get(id=house_id)
        except:
            return http.JsonResponse({"errno": "400", "errmsg": "该房子不存在"})
        order=Order.objects.create(
            user=user,
            house=house,
            begin_date=start_date,
            end_date=end_date,
            days=days,
            house_price=house.price,
            amount=house.price*days,
            status=0
        )
        content={
            "data": {
                "order_id": order.id
            },
            "errno": "0",
            "errmsg": "下单成功"
        }
        return http.JsonResponse(content)
        # user = models.ForeignKey("users.User", related_name="orders", on_delete=models.CASCADE, verbose_name="下订单的用户编号")
        # house = models.ForeignKey("homes.House", on_delete=models.CASCADE, verbose_name="预订的房间编号")
        # begin_date = models.DateField(null=False, verbose_name="预订的起始时间")
        # end_date = models.DateField(null=False, verbose_name="结束时间")
        # days = models.IntegerField(null=False, verbose_name="预订的总天数")
        # house_price = models.IntegerField(null=False, verbose_name="房屋单价")
        # amount = models.IntegerField(null=False, verbose_name="订单总金额")
        # status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=0, db_index=True, verbose_name="订单状态")
        # comment = models.TextField(null=True, verbose_name="订单的评论信息或者拒单原因")

    def get(self,request):
        role = request.GET.get('role')
        username = request.session.get('username')
        try:
            user = User.objects.get(mobile=username)
        except:
            return http.JsonResponse({"errno": "4101", "errmsg": "未登录"})
        try:
            houses = House.objects.filter(user_id=user.id)
        except:
            return http.JsonResponse({"errno": "400", "errmsg": "该房子不存在"})
        order_list=[]
        if role=='custom':
            orders=Order.objects.filter(user_id=user.id)
            for order in orders:
                order_dict={
                    "amount": order.amount,
                    "comment": order.comment,
                    "ctime": order.create_time,
                    "days": order.days,
                    "end_date": order.end_date,
                    "img_url": (QINIU_URL+str(order.house.index_image_url)),
                    "order_id": order.id,
                    "start_date": order.begin_date,
                    "status": order.ORDER_STATUS_ENUM[order.status],
                    "title": order.house.title
                }
                order_list.append(order_dict)
            content={
                "data": {
                    "orders": order_list
                },
                "errmsg": "OK",
                "errno": "0"
            }
            return http.JsonResponse(content)

        else:
            for house in houses:
                orders = Order.objects.filter(house_id=house.id)
                for order in orders:
                    order_dict={
                        "amount": order.amount,
                        "comment": order.comment,
                        "ctime": order.create_time,
                        "days": order.days,
                        "end_date": order.end_date,
                        "img_url": (QINIU_URL+str(order.house.index_image_url)),
                        "order_id": order.id,
                        "start_date": order.begin_date,
                        "status": order.ORDER_STATUS_ENUM[order.status],
                        "title": order.house.title
                    }
                    order_list.append(order_dict)
            content={
                "data": {
                    "orders": order_list
                },
                "errmsg": "OK",
                "errno": "0"
            }
            return http.JsonResponse(content)

#订单状态
class OrderStatusView(View):
    def put(self,request,order_id):
        action=json.loads(request.body.decode()).get('action')
        try:
            order=Order.objects.get(id=order_id)
        except:
            return http.JsonResponse({"errno": "400", "errmsg": "该订单不存在"})
        try:
            house = House.objects.get(id=order.house.id)
        except:
            return http.JsonResponse({"errno": "400", "errmsg": "该房子不存在"})
        if action=='accept':
            # print('action:accept')
            order.status=3
            house.order_count=house.order_count+1
            try:
                order.save()
                house.save()
            except:
                return http.JsonResponse({"errno": "400", "errmsg": "接单失败"})
        else:
            # print('action:no-accept')
            reason = json.loads(request.body.decode()).get('reason')
            # print(reason)
            order.status = 6
            order.comment=reason
            try:
                order.save()
            except:
                return http.JsonResponse({"errno": "400", "errmsg": "拒单失败"})

        return http.JsonResponse({"errno": "0", "errmsg": "操作成功"})


class OrderCommentView(View):
    def put(self,request,order_id):
        comment = json.loads(request.body.decode()).get('comment')
        try:
            order=Order.objects.get(id=order_id)
        except:
            return http.JsonResponse({"errno": "400", "errmsg": "该订单不存在"})
        order.comment=comment
        order.status=4
        try:
            order.save()
        except:
            return http.JsonResponse({"errno": "400", "errmsg": "评论失败"})


        return http.JsonResponse({"errno": "0", "errmsg": "评论成功"})






